package com.example.springscuritydemo.aspect;

import com.example.springscuritydemo.annotation.Log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName DemoAspect
 * @Description TODO
 * @Author 张晓焕
 * @Date 2022/5/20 14:23
 * @Version 1.0
 */
@Aspect
@Component
public class DemoAspect {

    @Pointcut("@annotation(com.example.springscuritydemo.annotation.Log)")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object afterAdvice(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知");
        final MethodSignature signature =(MethodSignature) point.getSignature();
        final Method method = signature.getMethod();
        final Log annotation = method.getAnnotation(Log.class);
        final String name = point.getTarget().getClass().getName();
        System.out.println(annotation.effects());

        //执行真正业务逻辑
        Object result = point.proceed();

        System.out.println(method.getName());
        System.out.println(name);

        return result;
    }

    @After("pointCut()")
    public void after() {
        System.out.println("后置通知");
    }

    @AfterReturning(pointcut = "pointCut()")
    public void afterReturning() {
        System.out.println("AfterReturning");
    }

    @Before("pointCut()")
    public void before(){
        System.out.println("前置通知");
    }

    @AfterThrowing(pointcut = "pointCut()")
    public void afterThrowing() {
        System.out.println("AfterThrowing");
    }
}
